
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NEM.Networking_Classes;
using System.Drawing.Design;
using System.Windows.Forms.Design;
using System.Windows.Forms;

namespace NEM
{
    public class NetworkCardConverter : TypeConverter
    {
        #region Properties
        public static List<NetworkConnection> _NetworkCards = new List<NetworkConnection>();
        #endregion

        #region Overrides
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(string))
            {
                return true;
            }

            return base.CanConvertFrom(context, sourceType);
        }
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (value.GetType() == typeof(string))
            {
                if (value.ToString() != string.Empty)
                {
                    NetworkConnection lvNetworkCard = new NetworkConnection();

                    lvNetworkCard = _NetworkCards.Where(a => a.ToString() == value.ToString()).FirstOrDefault();

                    return lvNetworkCard;
                }
            }


            return base.ConvertFrom(context, culture, value);
        }

        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }
        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            return true;
        }

        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            if (context != null && context.Instance != null && context.PropertyDescriptor != null)
            {
               //Current Selected Object Show Display This one
            }

            return new StandardValuesCollection(_NetworkCards);
        }
        #endregion

    }

    class FancyStringEditor : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            var svc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
            if (svc != null)
            {
                using (var frm = new Form { Text = "String Editor", AcceptButton = null })
                using (var txt = new TextBox { Text = (string)value, Dock = DockStyle.Fill, Multiline = true })
                using (var btnOk = new Button { Text = "OK", Dock = DockStyle.Bottom })
                {
                    frm.Controls.Add(txt);
                    frm.Controls.Add(btnOk);
                    //frm.AcceptButton = btnOk;
                    btnOk.DialogResult = DialogResult.OK;
                    if (svc.ShowDialog(frm) == DialogResult.OK)
                    {
                        value = txt.Text;
                    }
                }
            }
            return value;
        }
    }

}
